﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="editArticle.aspx.cs" Inherits="__editArticle" %>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
		<script src='oosres://libs/jquery/1.7.1/jquery.min.js' type='text/javascript'></script>
		<script src='oosres://libs/jqueryui/1.8.22/jquery-ui-minDD.js' type='text/javascript'></script>
		<script src='oosres://libs/jquery_plugins/blockUI/jquery.blockUI-2.43.js' type='text/javascript'></script>

		<style>
			.actionButton
			{
				float:right;
			}
			
			.oosMsgPanel
			{
				display:none;
			}
			
			.placeholder
			{
				height:4em;
			}
			
			.deletePageLink
			{
				text-decoration:underline;
			}
			
			.deleteSectionLink
			{
				text-decoration:underline;
			}
		</style>

		<script type='text/javascript'>/* <![CDATA[ */
			oos.dom.onReady(function () {

			//add click event to delete section links
			$('.deleteSectionLink').each(function(index) {
				var parts = $(this).attr('id').split('_');
				var articleID = parts[0];
				var pageID = parts[1];
				var sectionID = parts[2];
				var title = parts[3];

				$(this).bind('click',function(){
					if(confirm('<octo:Literal Mode="PassThrough" Text="<%$ Resources:Main, shared_DeleteSection %>" runat="server" />'.replace("{0}", title)))
					{
						$.blockUI( { message:'<octo:Literal Text="<%$ Resources:Main, shared_BlockMessage %>" Mode="PassThrough" runat="server" />'});
						$.ajax({
							type: 'POST',
							url: '<octo:Literal ID="deleteSectionUrl" Mode="PassThrough" runat="server" />',
							cache: false,
							data: JSON.stringify({articleID:articleID, pageID:pageID, sectionID: sectionID }),
							contentType: 'application/json; charset=utf-8',
							dataType: 'json',
							success: function (data) {
								var element = document.getElementById(pageID+ '_' +sectionID);
								element.parentNode.removeChild(element);					
								$.unblockUI();
							},
							error: function () {
								alert('<octo:Literal Mode="PassThrough" Text="<%$ Resources:Main, shared_AjaxError %>" runat="server" />');
								location.reload();
							}
						});
					}
				});//bind
			});//each fn

			//add click event to delete page links
			$('.deletePageLink').each(function(index) {
				var parts = $(this).attr('id').split('_');
				var articleID = parts[0];
				var pageID = parts[1];
				var title = parts[2];

				$(this).bind('click',function()
				{
					if(confirm('<octo:Literal Mode="PassThrough" Text="<%$ Resources:Main, shared_DeletePage %>" runat="server" />'.replace("{0}", title)))
					{
						$.blockUI({ message:'<octo:Literal Text="<%$ Resources:Main, shared_BlockMessage %>" Mode="PassThrough" runat="server" />'});
						$.ajax(
						{
							type: 'POST',
							url: '<octo:Literal ID="deletePageUrl" Mode="PassThrough" runat="server" />',
							cache: false,
							data: JSON.stringify({ articleID:articleID, pageID:pageID }),
							contentType: 'application/json; charset=utf-8',
							dataType: 'json',
							success: function(data) 
							{
								var element = document.getElementById(pageID);
								element.parentNode.removeChild(element);					
								$.unblockUI();
							},
							error: function() 
							{
								alert('<octo:Literal Mode="PassThrough" Text="<%$ Resources:Main, shared_AjaxError %>" runat="server" />');
								location.reload();
							}
						});//ajax({
					}//confirm
				});//bind
			});//each fn

			var select = document.getElementById("publishedSelect");
			select.addEventListener('change', setPublishVision, false);
			setPublishVision();

			function setPublishControlsDisabled(disableStart, disableStop)
			{
				var publishStartAtRadio = document.getElementById("startAtRadio");
				var publishStopAtRadio = document.getElementById("stopAtRadio");
				var publishStartNowRadio = document.getElementById("startNowRadio");
				var publishStopNeverRadio = document.getElementById("stopNeverRadio");
				var publishStartTime = document.getElementById("publishStartDate");
				var publishStopTime = document.getElementById("publishStopDate");

				if(disableStart)
				{
					publishStartAtRadio.disabled = true;
					publishStartNowRadio.disabled = true;
					publishStartTime.disabled = true;
				}
				else
				{
					publishStartAtRadio.disabled = false;
					publishStartNowRadio.disabled = false;
					publishStartTime.disabled = false;
				}

				if(disableStop)
				{
					publishStopNeverRadio.disabled = true;
					publishStopAtRadio.disabled = true;
					publishStopTime.disabled = true;
				}
				else
				{
					publishStopNeverRadio.disabled = false;
					publishStopAtRadio.disabled = false;
					publishStopTime.disabled = false;
				}
			}


			function setPublishVision() {
				var publishStarted = document.getElementById("publishStarted");
				var publishStopped = document.getElementById("publishStopped");

				if(select.value == 'false')
				{
					setPublishControlsDisabled(true, true);
				}
				else
				{
					if(publishStarted.value == 'false')
					{
						//не началось
						setPublishControlsDisabled(false, false);
					}
					else
					{
						if(publishStopped.value == 'false')
						{
							//началось и не кончилось
							setPublishControlsDisabled(true, false);
						}
						else
						{
							//кончилось
							setPublishControlsDisabled(true, true);
						}
					}
				}
			}

			$('.oos_t1').sortable(
			{
				axis: 'y',
				cursor: 'move',
				helper: 'clone',
				opacity: 0.6,
				placeholder: 'placeholder',
				tolerance: 'pointer',
				start: function (event, ui) {
					ui.helper.css('border-top', '1px solid black');
					ui.helper.css('border-bottom', '1px solid black');
				},

				update: function (event, ui) {
					$.blockUI( { message: '<octo:Literal Mode="PassThrough" Text="<%$ Resources:Main, shared_BlockMessage %>" runat="server" />' });
					var artID = document.getElementById('articleID').value;
					var s = $(this).sortable('toArray').toString();
					$.ajax({
						type: 'POST',
						url: '<octo:Literal ID="ajaxSectionsServiceUrl" Mode="PassThrough" runat="server" />',
						cache: false,
						data: JSON.stringify({ sectionOrder: s, articleID:artID }),
						contentType: 'application/json; charset=utf-8',
						dataType: 'json',
						success: function (data) {
							$.unblockUI();
						},
						error: function () {				
							alert('<octo:Literal Mode="PassThrough" Text="<%$ Resources:Main, shared_ErrorMsg %>" runat="server" />');
							location.reload();
						}
					});
				}
			});

			$('#pagesPlaceholder').sortable(
			{
				axis: 'y',
				cursor: 'move',
				helper: 'original',
				opacity: 0.6,
				tolerance: 'pointer',
				placeholder: 'placeholder',
				start: function (event, ui) {
					ui.helper.css('border-top', '1px solid black');
					ui.helper.css('border-bottom', '1px solid black');
				},

				update: function (event, ui) {
					$.blockUI( { message:'<octo:Literal Mode="PassThrough" Text="<%$ Resources:Main, shared_BlockMessage %>" runat="server" />' });
					var artID = document.getElementById('articleID').value;
					var s = $(this).sortable('toArray').toString();
					$.ajax({
						type: 'POST',
						url: '<octo:Literal ID="ajaxPagesServiceUrl" Mode="PassThrough" runat="server" />',
						cache: false,
						data: JSON.stringify({ pagesOrder: s, articleID:artID }),
						contentType: 'application/json; charset=utf-8',
						dataType: 'json',
						success: function (data) {
							$.unblockUI();
						},
						error: function () {
							alert('<octo:Literal Mode="PassThrough" Text="<%$ Resources:Main, shared_ErrorMsg %>" runat="server" />');
							location.reload();
						}
					});
				}
			}
			)

			
			if(document.referrer.indexOf('editArticle.aspx') != -1)
			{
				$(".oosMsgPanel").show();
				setTimeout(function(){
					$(".oosMsgPanel").fadeOut(2000, null);
					}, 1500);
			}
		});

//		var myApp = {};
//		myApp.deleteSection = function(articleID, pageID, sectionID, title){
//			if(confirm('<octo:Literal Mode="PassThrough" Text="<%$ Resources:Main, shared_DeleteSection %>" runat="server" />'.replace("{0}", title)))
//			{
//				$.blockUI( { message:'<octo:Literal Text="<%$ Resources:Main, shared_BlockMessage %>" Mode="PassThrough" runat="server" />'});
//				$.ajax({
//					type: 'POST',
//					url: '<octo:Literal ID="deleteSectionUrl2" Mode="PassThrough" runat="server" />',
//					cache: false,
//					data: JSON.stringify({articleID:articleID, pageID:pageID, sectionID: sectionID }),
//					contentType: 'application/json; charset=utf-8',
//					dataType: 'json',
//					success: function (data) {
//						var element = document.getElementById(pageID+ '_' +sectionID);
//						element.parentNode.removeChild(element);					
//						$.unblockUI();
//					},
//					error: function () {
//						alert('<octo:Literal Mode="PassThrough" Text="<%$ Resources:Main, shared_AjaxError %>" runat="server" />');
//						location.reload();
//					}
//				});
//			}
//		}

//		myApp.deletePage = function(articleID, pageID, title){
//			if(confirm('<octo:Literal Mode="PassThrough" Text="<%$ Resources:Main, shared_DeletePage %>" runat="server" />'.replace("{0}", title)))
//			{
//				$.blockUI( { message:'<octo:Literal Text="<%$ Resources:Main, shared_BlockMessage %>" Mode="PassThrough" runat="server" />'});
//				$.ajax({
//					type: 'POST',
//					url: '<octo:Literal ID="deletePageUrl2" Mode="PassThrough" runat="server" />',
//					cache: false,
//					data: JSON.stringify({ articleID:articleID, pageID:pageID }),
//					contentType: 'application/json; charset=utf-8',
//					dataType: 'json',
//					success: function (data) {
//						var element = document.getElementById(pageID);
//						element.parentNode.removeChild(element);					
//						$.unblockUI();
//					},
//					error: function () {
//						alert('<octo:Literal Mode="PassThrough" Text="<%$ Resources:Main, shared_AjaxError %>" runat="server" />');
//						location.reload();
//					}
//				});
//			}
//		}

		/* ]]> */
	</script>

</head>
<body>
    <form id="form1" runat="server">
		<octo:FormView runat="server">
			<octo:FormRow Label="<%$ Resources:Main, shared_Categories %>" OnServerValidate="CategoriesValidate" runat="server">
				<octo:Select ID="categories1" Required="true" runat="server">
					<octo:Option Value="0" runat="server" />
				</octo:Select>
				<octo:CheckBox ID="featured1" Text="<%$ Resources:Main, editArticle_Featured %>" runat="server" /><br/>
				<octo:Select ID="categories2" runat="server">
					<octo:Option Value="0" runat="server" />
				</octo:Select>
				<octo:CheckBox ID="featured2" Text="<%$ Resources:Main, editArticle_Featured %>" runat="server" /><br/>
				<octo:Select ID="categories3" runat="server">
					<octo:Option Value="0" runat="server" />
				</octo:Select>
				<octo:CheckBox ID="featured3" Text="<%$ Resources:Main, editArticle_Featured %>" runat="server" /><br/>
			</octo:FormRow>

			<octo:FormRow Label="<%$ Resources:Main, shared_Author %>" runat="server">
				<octo:InputText ID="author" Required="true" Style="width:100%" MaxLength="100" runat="server" />
			</octo:FormRow>

			<octo:FormRow Label="<%$ Resources:Main, shared_Title %>" runat="server">
				<octo:InputText ID="title" Required="true" Style="width:100%" MaxLength="100" runat="server" />
			</octo:FormRow>

			<octo:FormRow Label="<%$ Resources:Main, editArticle_Preface %>" VerticalLayout="true" runat="server">
				<octo:TextArea ID="preface" Autogrow="true" MaxLength="400" runat="server" />
			</octo:FormRow>

			<octo:FormRow Label="<%$ Resources:Main, editArticle_PublishStatus %>" runat="server">
				<octo:Select ID="publishedSelect" runat="server">
					<octo:Option Value="false" Text="<%$Resources:Main, shared_NotPublished %>" runat="server" />
					<octo:Option Value="true" Text="<%$Resources:Main, shared_Published %>" runat="server" />
				</octo:Select>
			</octo:FormRow>

			<octo:FormRow Label="<%$ Resources:Main, shared_PublishStarts %>" OnServerValidate="PublishStartDateValidate" runat="server">
				<asp:RadioButton ID="startNowRadio" Text="<%$ Resources:Main, shared_Now %>" Checked="true" GroupName="start" runat="server" />
				<br/>
				<asp:RadioButton ID="startAtRadio" Text="<%$ Resources:Main, shared_At %>" GroupName="start" runat="server" />
				<octo:InputDateTime ID="publishStartDate" Step="1" runat="server" />
			</octo:FormRow>

			<octo:FormRow Label="<%$ Resources:Main, shared_PublishStops %>" OnServerValidate="PublishStopDateValidate" runat="server">
				<asp:RadioButton ID="stopNeverRadio" Text="<%$ Resources:Main, shared_Never %>" Checked="true" GroupName="stop" runat="server" />
				<br/>
				<asp:RadioButton ID="stopAtRadio" Text="<%$ Resources:Main, shared_At %>" GroupName="stop" runat="server" />
				<octo:InputDateTime ID="publishStopDate" Step="1" runat="server" />
			</octo:FormRow>

			<octo:FormRow runat="server">
				<octo:Submit ID="submitBtn" OnClick="SubmitClick" runat="server" />
			</octo:FormRow>
		</octo:FormView>

		<octo:MessageBox ID="savedMsg" MessageType="Info" runat="server" />
    </form>


	<div ID="pagesPlaceholder" runat="server" />
	<input id="publishStarted" type="hidden" value="false" runat="server" />
	<input id="publishStopped" type="hidden" value="false" runat="server" />
	<input id="articleID" type="hidden" runat="server" />
</body>
</html>
